package com.yxh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yxh.base.PageEntity;
import com.yxh.dao.FineTypeMapper;
import com.yxh.dao.WordbookMapper;
import com.yxh.exception.UnitException;
import com.yxh.exception.WordbookException;
import com.yxh.pojo.FineType;
import com.yxh.service.FineTypeService;
import com.yxh.util.SnowflakeIdWorker;
import com.yxh.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.util.List;

import static com.yxh.util.APIErrorCode.*;
import static com.yxh.util.APIErrorCode.PARAMS_INVALID;

/**
 * Created by 32988 on 2018/6/2.
 */
@Service("fineTypeService")
@Transactional
public class FineTypeServiceImpl implements FineTypeService {

    @Autowired
    FineTypeMapper fineTypeMapper;

    @Autowired
    WordbookMapper wordbookMapper;

    @Override
    public PageInfo getInformation(FineType fineType, Integer limit, Integer offset) {
        PageHelper.startPage(limit, offset);
        try {
            if (!StringUtils.isEmpty(fineType.getFineTypeName())) {
                fineType.setFineTypeName(new String(fineType.getFineTypeName().getBytes("iso8859-1"), "utf-8"));
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        List<FineType> fineTypeList = fineTypeMapper.selectByFineType(fineType);
        PageInfo pageInfo = new PageInfo(fineTypeList);
        pageInfo.setList(fineTypeList);
        return pageInfo;
    }

    @Override
    public int save(FineType fineType) {
        fineType.setIsDelete("2");
        int count = fineTypeMapper.insertSelective(fineType);
        if (count == 0) {
            throw new WordbookException(INTERNAL_ERROR);
        }
        return count;
    }

    @Override
    public int update(FineType record) {
        int count = fineTypeMapper.updateByPrimaryKeySelective(record);
        if (count == 0) {
            throw new WordbookException(PARAMS_INVALID);
        }
        return count;
    }

    @Override
    public int delete(String id) {
        FineType fineType = fineTypeMapper.selectByPrimaryKey(id);
        if ("1".equals(fineType.getIsDelete())) {
            throw new WordbookException(NOT_DELETE_ERROR);
        }
        int count = wordbookMapper.checkFineType(fineType.getFineTypeId());
        if (count > 0) {
            throw new WordbookException(NOT_DELETE_ERROR);
        }
        if (!StringUtil.isEmpty(id)) {
            String[] arr = id.split(",");
            count = fineTypeMapper.deleteByIds(arr);
            if (count == 0) {
                throw new UnitException(PARAMS_INVALID);
            }
        }
        return count;


    }

    @Override
    public void batchDelete(List<FineType> records) {

    }

    @Override
    public FineType get(String id) {
        return null;
    }

    @Override
    public FineType get(FineType record) {
        return null;
    }

    @Override
    public List<FineType> getList(FineType record) {
        return null;
    }

    @Override
    public List<FineType> getAllList(FineType record) {
        return null;
    }

    @Override
    public List<FineType> getListPage(PageEntity<FineType> record) {
        return null;
    }
}
